﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CreateObject
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DataGridViewComboBoxColumn cboType = (DataGridViewComboBoxColumn)dgvParametre.Columns[2];
            cboType.Items.Add("String");
            cboType.Items.Add("int");
            cboType.Items.Add("Double");
            cboType.Items.Add("Decimal");
            cboType.Items.Add("DateTime");
            cboType.Items.Add("Boolean");
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            List<Objet> parametre = new List<Objet>();
            String table = "\tpublic string GetTable()\n\t{\n\t\treturn \"" + txtNom.Text + "\";\n\t}\n";
            String clefIsAuto = "\tpublic bool clefIsAuto()\n\t{\n\t\treturn " + cbAuto.Checked.ToString().ToLower() + ";\n\t}\n";
            String getParam = "\tpublic List<Type[]> GetParam(int nb)\n\t{\n\t\tList<Type[]> rep = new List<Type[]>();\n\t\tforeach (System.Reflection.ConstructorInfo con in typeof(" + txtNom.Text + ").GetConstructors())\n\t\t{\n\t\t\tif (con.GetParameters().Count() == nb)\n\t\t\t\trep.Add(Array.ConvertAll(con.GetParameters(), item => (Type)item.ParameterType));\n\t\t}\n\t\treturn rep;\n\t}\n";
            String parameters = "";
            String rep = "public class " + txtNom.Text + " : SQLExec\n{\n";
            String constructor = "\tpublic " + txtNom.Text + "(";
            String sqlConstructor = "\tpublic static SQLConstructor GetConstruction1(){\n\t\treturn new SQLConstructor(new Type[] {";
            String select = "select ";

            for (int a = 0; a < dgvParametre.Rows.Count - 1; a++)
            {
                Objet o = new Objet();
                o.Nom = dgvParametre[0,a].Value.ToString();
                o.NomSQL = dgvParametre[1, a].Value.ToString();
                switch (dgvParametre[2, a].Value.ToString())
                {
                    case "String":
                        o.Valeur = typeof(String);
                        break;
                    case "int":
                        o.Valeur = typeof(int);
                        break;
                    case "Double":
                        o.Valeur = typeof(Double);
                        break;
                    case "Decimal":
                        o.Valeur = typeof(Decimal);
                        break;
                    case "DateTime":
                        o.Valeur = typeof(DateTime);
                        break;
                    case "Boolean":
                        o.Valeur = typeof(bool);
                        break;
                    default:
                        o.Valeur = typeof(String);
                        break;
                }
                o.Clef = dgvParametre[3, a].Value == null ? false : (bool)dgvParametre[3, a].Value;
                parametre.Add(o);

                parameters += "\tpublic " + o.Valeur.ToString() + " " + o.Nom + " { get; set; }\n";
                constructor += o.Valeur.ToString() + " " + o.Nom + ", ";
                sqlConstructor += "typeof(" + o.Valeur.ToString() + "), ";
                select += o.NomSQL + ", ";
            }

            constructor = constructor.Substring(0, constructor.Length - 2) + ")\n\t{\n";
            select = select.Substring(0, select.Length - 2) + " from " + txtNom.Text;
            sqlConstructor = sqlConstructor.Substring(0, sqlConstructor.Length - 3) + "}, \"" + select + "\");\n\t}\n";
            
            String elements = "\tpublic SQLParameter[] GetElements()\n\t{\n\t\tSQLParameter[] p = new SQLParameter[" + parametre.Count(k => k.Clef == false) + "];\n";
            String key = "\tpublic SQLParameter[] GetClef()\n\t{\n\t\tSQLParameter[] p = new SQLParameter[" + parametre.Count(k => k.Clef == true) + "];\n";
            int indexKey = 0, indexElements = 0;
            foreach (Objet o in parametre)
            {
                constructor += "\t\tthis." + o.Nom + " = " + o.Nom + ";\n";
                if (o.Clef)
                {
                    key += "\t\tp[" + indexKey + "] = new SQLParameter(\"" + o.NomSQL + "\", " + o.Nom + ");\n";
                    indexKey++;
                }
                else
                {
                    elements += "\t\tp[" + indexElements + "] = new SQLParameter(\"" + o.NomSQL + "\", " + o.Nom + ");\n";
                    indexElements++;
                }
            }
            elements += "\t\treturn p;\n\t}\n";
            key += "\t\treturn p;\n\t}\n";
            constructor += "\t}\n";

            rep += parameters + constructor + key + elements + table + clefIsAuto + sqlConstructor + getParam + "}";
            rtxtReponse.Text = rep;
        }

        private void dgvParametre_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && cbNom.Checked)
            {
                dgvParametre[1, e.RowIndex].Value = dgvParametre[0, e.RowIndex].Value;
            }
        }
    }

    public class Objet
    {
        public String Nom { get; set; }
        public String NomSQL { get; set; }
        public Type Valeur { get; set; }
        public bool Clef { get; set; }

        public Objet(string nom, string nomSQL, Type val, bool clef)
        {
            this.Clef = clef;
            this.Nom = nom;
            this.NomSQL = nomSQL;
            this.Valeur = val;
        }
        public Objet()
        {
            this.Clef = false;
            this.Nom = "";
            this.NomSQL = "";
            this.Valeur = typeof(String);
        }
    }
}